(function() {
	var goodsTotals			= "[data-role='goods_total']",		//所有汇总块
		goodsCounts			= "[data-role='goods_count']",		//所有数据块
		subtractButton		= "[data-role='subtract']",			//所有减少按钮
		addButton			= "[data-role='add']",				//所有增加按钮
		unitPrice			= "[data-type='unit_price']",		//商品单价
		allInputs			= "[data-type*='male']",			//全部性别购买数量输入框
		maleInputs			= "[data-type^='male']",			//全部男性购买数量输入框
		femaleInputs		= "[data-type^='female']",			//全部女性购买数量输入框
		allCount			= "[data-count='all']",				//全部服饰购买数量统计
		maleCount			= "[data-count='male']",			//男性服饰购买数量统计
		femaleCount			= "[data-count='female']",			//女性服饰购买数量统计
		moneyCount			= "[data-count='money']",			//服饰购买金额统计
		moneyTotal			= "[data-total='money']",			//服饰购买金额统计
		delayer				= null,								//延迟器
		succession			= null;								//连续运算器
	
	/*function getParent(thisObj) {
		var parent = thisObj.parent();
		
		while (true) {
			if (parent.attr("data-role") == "goods_count") {
				console.log(parent);
				return parent;
			}
			
			if (parent[0].tagName == "html") {
				return false;
			}
			
			parent = parent.parent();
		}
	}*/
	
	
	/*console.log(total($(".wrap")));
	function total(wrap) {
		var son = wrap.find(""+moneyTotal);
		var totalPrice=0;
		son.each(function(){
			totalPrice+=parseFloat($(this).data("money"));
		});
//		if(wrap.find(">"+goodsTotals)){
//			return totalPrice+=total(wrap.find(">"+goodsTotals));
//		}else{
			return totalPrice;
//		}
	}*/
	
	function updateTotal(thisObj) {
		thisObj.parents(goodsTotals).each(function() {
			console.log($(this).find(moneyTotal).last());
		});
	}
	
	/*显示统计数据*/
	function showCountData(thisCount) {
		var maleCountNum	= 0,
			femaleCountNum	= 0;
		
		thisCount.find(maleInputs).each(function() {
			maleCountNum += parseInt($(this).val());
		});
		
		thisCount.find(femaleInputs).each(function() {
			femaleCountNum += parseInt($(this).val());
		});
		
		thisCount.find(maleCount).html(maleCountNum);
		thisCount.find(femaleCount).html(femaleCountNum);
		thisCount.find(allCount).html(maleCountNum + femaleCountNum);
		thisCount.find(moneyCount).html(((maleCountNum + femaleCountNum) * parseInt($(unitPrice).html())).toFixed(2));
		
		thisCount.parents(goodsTotals).each(function() {
			var moneyTotal = 0;
			
			$(this).find(moneyCount).each(function() {
				moneyTotal += parseFloat($(this).html());
				console.log(moneyTotal);
			});
			
			$(this).find(moneyTotal).last().html(moneyTotal);
		});
		
		/*if (thisCount.parents(goodsTotals).length>0) {
			thisCount.parents(goodsTotals).each(function() {
				$(this).find(moneyTotal).html(new Date());
			});
		}*/
		
		//updateTotal($(thisCount));
	}
	
	/*减少数量*/
	$(subtractButton).mousedown(function() {
		var thisCount	= $(this).parents(goodsCounts),
			thisInput	= $(this).parent().find("input"),
			result		= parseInt(thisInput.val());
		
		if (result>0) {
			thisInput.val(--result);
			showCountData(thisCount);
		}
		
		delayer = setTimeout(function() {
			succession = setInterval(function() {
				if (result>0) {
					thisInput.val(--result);
					showCountData(thisCount);
				}
			}, 20);
		}, 1000);
	}).mouseup(function() {
		clearTimeout(delayer);
		clearInterval(succession);
	}).mouseout(function() {
		clearTimeout(delayer);
		clearInterval(succession);
	});
	
	/*增加数量*/
	$(addButton).mousedown(function() {
		var thisCount	= $(this).parents(goodsCounts),
			thisInput	= $(this).parent().find("input"),
			result		= parseInt(thisInput.val());
		
		if (result<999) {
			thisInput.val(++result);
			showCountData(thisCount);
		}
		
		delayer = setTimeout(function() {
			succession = setInterval(function() {
				if (result<999) {
					thisInput.val(++result);
					showCountData(thisCount);
				}
			}, 20);
		}, 1000);
	}).mouseup(function() {
		clearTimeout(delayer);
		clearInterval(succession);
	}).mouseout(function() {
		clearTimeout(delayer);
		clearInterval(succession);
	});
	
	/*禁止输入非合法数字的字符*/
	$(allInputs).focus(function() {
		$(this).select();
	}).keyup(function(e) {
		$(this).val($(this).val().replace(/\D|^0/g, ""));
    }).blur(function() {
    	$(this).val().length<=0 ? $(this).val(0) : "";
    	showCountData($(this).parents(goodsCounts));
    }).bind("paste", function(e){
		e.preventDefault();
		return false;
    }).on("drop",function(e){
    	e.preventDefault();
    	return false;
    });
    
    /*初始化设置*/
	$(allInputs).attr("maxlength", 3).val(0);
	$(maleCount).html(0);
	$(femaleCount).html(0);
	$(allCount).html(0);
	$(moneyCount).html(0);
	
	
	$("[data-role='edit']").click(function() {
		$("#shanchu1").show();
	});
	
	$("[data-role='close']").click(function() {
		$(".woddz_mask").hide();
	});

	var tip = null;
	$("#done").on("click", function () {
		clearTimeout(tip);
		$("#shanchu3").show();
		tip = setTimeout(function () {
            $("#shanchu3").hide();
        }, 2000);
    });
})();